package com.youlu.campus.web.community.service;

import com.google.common.collect.Lists;
import com.youlu.campus.base.interactive.vo.ActivitySignUpRequest;
import com.youlu.campus.entity.*;
import com.youlu.campus.entity.commission.UserParticipateDistributorChannelInfo;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.activity.UserParticipateActivityInfoService;
import com.youlu.campus.service.commission.UserParticipateDistributorChannelInfoService;
import com.youlu.campus.service.feed.ActivityFeedInfoService;
import com.youlu.campus.service.visitor.UserInfoExtendService;
import com.youlu.campus.web.common.base.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Slf4j
@Service
public class WxMiniContestActivitySignUpService {

    @Autowired
    private UserInfoExtendService userInfoExtendService;

    @Autowired
    private UserParticipateActivityInfoService userParticipateActivityInfoService;

    @Autowired
    private ActivityFeedInfoService activityFeedInfoService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private UserParticipateDistributorChannelInfoService userParticipateDistributorChannelInfoService;


    public Boolean findJoinedSignUpByUserIdAndActivityId(UserPO currentUser,String activityId,String activityChannelId){
        log.info("【参与活动报名】WxMiniContestActivitySignUpService.findJoinedSignUpByUserIdAndActivityId userId ->{} | " +
                        "activityId ->{},activityChannelId:{}",currentUser.getId(),
                activityId,activityChannelId);
        UserInfoExtend user = userInfoExtendService.findByUserId(currentUser.getId());
        if(Objects.isNull(user)){
            return Boolean.FALSE;
        }
        String name = user.getName();
        String universityId = user.getUniversityId();
        String mobile = user.getMobile();
        UserParticipateActivityInfo userSignUp = userParticipateActivityInfoService.findByUserIdActivityId(currentUser.getId(), activityId);
        if(Objects.nonNull(userSignUp)){
            return Boolean.TRUE;
        }
        log.info("【参与活动报名】WxMiniContestActivitySignUpService.findJoinedSignUpByUserIdAndActivityId 最新报名数据 userId ->{}" +
                        " |" +
                        " " +
                        "activityId ->{}",currentUser.getId(),
                activityId);
        userSignUp = userParticipateActivityInfoService.getJoinedLastByUserId(currentUser.getId());
        if(Objects.isNull(userSignUp)){
            log.info("【参与活动报名】WxMiniContestActivitySignUpService.findJoinedSignUpByUserIdAndActivityId 最新报名填写 userId " +
                            "->{}" +
                            " |" +
                            " " +
                            "activityId ->{}",currentUser.getId(),
                    activityId);
            return Boolean.FALSE;
        }
        if(Objects.nonNull(userSignUp)&&StringUtils.isBlank(userSignUp.getActivityId())){
            return Boolean.FALSE;
        }

        if(StringUtils.isAnyBlank(name,universityId,mobile)){
            UserPO saveUser = new UserPO();
            if(StringUtils.isBlank(name)){
                saveUser.setName(userSignUp.getName());
            }
            if(StringUtils.isBlank(mobile)){
                saveUser.setMobile(userSignUp.getMobile());
            }
            if(StringUtils.isBlank(universityId)){
                saveUser.setUniversityId(userSignUp.getUniversityId());
                saveUser.setUniversityName(userSignUp.getUniversityName());
            }
            saveUser.setH5SignUp(Boolean.TRUE);
            userInfoExtendService.saveOrUpdate(saveUser);
        }
        if(StringUtils.isNotBlank(mobile)){
            userSignUp.setMobile(mobile);
        }
        if(StringUtils.isNotBlank(universityId)){
            userSignUp.setUniversityId(universityId);
            userSignUp.setUniversityName(user.getUniversityName());
        }
        if(StringUtils.isNotBlank(name)){
            userSignUp.setName(name);
        }
        userSignUp.setActivityId(activityId);
        userSignUp.setAppId(currentUser.getAppId());
        userSignUp.setOpenId(currentUser.getOpenId());
        userSignUp.setUnionId(currentUser.getUnionId());
        userSignUp.setJoined(Boolean.TRUE);
        userSignUp.setAvatarUrl(currentUser.getAvatarUrl());
        userSignUp.setQaScore(0D);
        userSignUp.setCountryQaScore(0D);
        userSignUp.setProvinceQaScore(0D);
        userSignUp.setCaName("");
        userSignUp.setContestCaName("");
        userSignUp.setPlatform(currentUser.getCurrentPlatform());
        userSignUp.setActivityChannelId(activityChannelId);
        userParticipateActivityInfoService.saveOrUpdate(userSignUp);
        if(StringUtils.isNotBlank(activityChannelId)){//渠道报名
            UserParticipateDistributorChannelInfo updateDistributorChannelInfo = new UserParticipateDistributorChannelInfo();
            BeanUtils.copyProperties(userSignUp,updateDistributorChannelInfo);
            updateDistributorChannelInfo.setId(null);
            updateDistributorChannelInfo.setActivityChannelId(activityChannelId);
            userParticipateDistributorChannelInfoService.save(updateDistributorChannelInfo);
        }

        // 添加Feed流
        ActivityFeedInfo feedInfo = new ActivityFeedInfo();
        feedInfo.setActivityId(activityId);
        feedInfo.setActivityStatus(2);
        feedInfo.setStatus(true);
        feedInfo.setImageUr(RequestContext.getCurUser().getAvatarUrl());
        feedInfo.setContent(RequestContext.getCurUser().getNickName() + "报名活动");
        activityFeedInfoService.addActivityFeedInfo(activityId, 2, feedInfo);
        activityService.updateActivityJoinNum(activityId,1,new Date());
        userParticipateActivityInfoService.increJoinedCount(activityId, 1L);
        ActivityInfo activityInfo = activityService.findOne(activityId);
        if(Objects.isNull(activityInfo)){
            return Boolean.TRUE;
        }
        //宣传类
        if(Lists.newArrayList("publicity","continuous-daka").contains(activityInfo.getType())&&StringUtils.isBlank(activityChannelId)){
            ActivitySignUpRequest request = new ActivitySignUpRequest();
            request.setActivityId(activityId);
            request.setPlatform(currentUser.getCurrentPlatform());
            request.setAppId(currentUser.getAppId());
            request.setUserId(currentUser.getId());
            activityService.extSignUpActivity(request, currentUser);
        }

        return Boolean.TRUE;

    }


}
